package com.xiangxiao.rpan.file.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.xiangxiao.rpan.file.constant.ResponseCode;
import com.xiangxiao.rpan.file.constant.ResponseMessage;
import com.xiangxiao.rpan.file.consumer.IConsumerStorageClient;
import com.xiangxiao.rpan.file.dto.BigFileDto;
import com.xiangxiao.rpan.file.dto.JsonObject;
import com.xiangxiao.rpan.file.exception.FileException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/8/4 10:40
 */
@Slf4j
@RequestMapping("/api/file")
@RestController
public class UploadController {
  @Resource
  private IConsumerStorageClient consumerStorageClient;

  @PostMapping("/operator/upload/file")
  public JsonObject uploadFile(@RequestPart("file") MultipartFile file,
                               @RequestParam("fileMD5") String fileMD5, @RequestParam("fileSize") Long fileSize,
                               @RequestParam("totalChunks") int totalChunks, @RequestParam("currChunks") int currChunks) {
    try {
      JSONObject resp = consumerStorageClient.bigFileUploadMulti(file, fileMD5, fileSize, totalChunks, currChunks);
      System.out.println(resp);
      BigFileDto dto = resp.getObject("data", new TypeReference<BigFileDto>() {});
      return new JsonObject(ResponseMessage.OK, ResponseCode.OK, dto);
    } catch (FileException e) {
      e.printStackTrace();
      return new JsonObject(ResponseMessage.FILE_UP_FAIL, ResponseCode.FILE_UP_FAIL);
    }
  }

  @PostMapping("/operator/compressedFileUploadMulti/file")
  public JsonObject uploadCompressedFile() {
    return null;
  }

  @PostMapping("operation/bigFileUploadMulti/file")
  public JsonObject uploadSplitChunkFile() {
    return null;
  }

  @PostMapping("/operator/folderUploadMulti/file")
  public JsonObject uploadFolder() {
    return null;
  }


}
